﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Eflong.DataAccess
{
    public class HisAlarmDataDump
    {
        #region Field

        private int _batchSize;

        /// <summary>
        /// 数据拆分批次记录大小
        /// </summary>
        public int BatchSize
        {
            get { return _batchSize; }
            set { _batchSize = value; }
        }

        private string _dumpDir;

        /// <summary>
        /// 数据序列化存储的跟目录地址
        /// </summary>
        public string DumpDir
        {
            get { return _dumpDir; }
            set { _dumpDir = value; }
        }

        #endregion Field

        public HisAlarmDataDump( )
            : this( 60 , @"c:\\HistoryAlarmDataDumpDir" )
        {
        }

        public HisAlarmDataDump( int batchSize , string dumpDir )
        {
            _dumpDir = dumpDir;
            _batchSize = batchSize;
        }

        /// <summary>
        /// 序列化数据到硬盘
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        public void Save<T>( IEnumerable<T> data )
        {
            if ( data == null || data.Count( ) == 0 ) return;
            try
            {
                string dataType = string.Empty;
                if ( typeof( T ) == typeof( KW_HisCombinedAlarmData ) )
                    dataType = "KW_HisCombinedAlarmData";
                else if ( typeof( T ) == typeof( KW_HisDeviceFaultData ) )
                    dataType = "KW_HisDeviceFaultData";
                else if ( typeof( T ) == typeof( KW_HisSimuAlarmData ) )
                    dataType = "KW_HisSimuAlarmData";
                else if ( typeof( T ) == typeof( KW_HisSwitchAlarmData ) )
                    dataType = "KW_HisSwitchAlarmData";
                else { return; }

                int dataCount = data.Count( );
                int batchNum = dataCount / _batchSize + ( dataCount % _batchSize > 0 ? 1 : 0 );

                for ( int i = 0; i < batchNum; i++ )
                {
                    string json = string.Empty;
                    if ( i != batchNum - 1 )
                        json = JsonConvert.SerializeObject( data.Skip( i * _batchSize ).Take( _batchSize ) );
                    else
                        json = JsonConvert.SerializeObject( data.Skip( ( batchNum - 1 ) * _batchSize ) );

                    string dataDir = Path.Combine( _dumpDir , dataType );
                    if ( !Directory.Exists( dataDir ) )
                        Directory.CreateDirectory( dataDir );

                    string dataPath = Path.Combine( dataDir , DateTime.Now.ToString( "yyyyMMddHHmmssfff" ) + i + ".json" );
                    using ( FileStream fileStream = new FileStream( dataPath , FileMode.Append , FileAccess.Write , FileShare.Read ) )
                    using ( StreamWriter streamWriter = new StreamWriter( fileStream , Encoding.UTF8 ) )
                    {
                        streamWriter.Write( json );
                        streamWriter.Flush( );
                    }
                }
            }
            catch ( Exception ex )
            {
                Console.WriteLine( ex.Message );
            }
        }
    }
}